package com.wr.recruit.service.service.impl;

import com.wr.recruit.service.dao.dbo.MessageDbo;
import com.wr.recruit.service.dao.dbo.RoleDbo;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.dao.repo.MessageRepo;
import com.wr.recruit.service.dao.repo.RoleRepo;
import com.wr.recruit.service.dao.repo.UserRepo;
import com.wr.recruit.service.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Objects;

import static com.wr.recruit.service.utils.Constants.INVESTIGATOR;

@Service
public class MessageServiceImpl implements MessageService {

    private MessageRepo messageRepo;
    private UserRepo userRepo;
    private RoleRepo roleRepo;

    @Autowired
    public MessageServiceImpl(MessageRepo messageRepo,
                              UserRepo userRepo,
                              RoleRepo roleRepo) {
        this.messageRepo = messageRepo;
        this.userRepo = userRepo;
        this.roleRepo = roleRepo;
    }

    @Override
    public MessageDbo createMessage(MessageDbo messageDbo) {
        return messageRepo.save(messageDbo);
    }

    @Override
    public MessageDbo updateMessage(MessageDbo messageDbo) {
        return messageRepo.save(messageDbo);
    }

    @Override
    public Boolean deleteMessage(String userName, Integer messageId) {
        UserDbo allByUserName = userRepo.findAllByUserName(userName);
        if (Objects.isNull(allByUserName))
            return false;
        RoleDbo allById = roleRepo.findAllById(allByUserName.getRoleId());
        if (Objects.isNull(allById))
            return false;
        if (INVESTIGATOR.equals(allById.getRoleCode())){
            MessageDbo message = messageRepo.findAllById(messageId);
            message.setRevoked(true);
            messageRepo.save(message);
            return true;
        }
        return false;
    }

    @Override
    public Page<MessageDbo> getMessageList(Double salaryMin, Double salaryMax, String companyCode, Integer pageSize, Integer pageNum) {
        Specification<MessageDbo> specification = new Specification<MessageDbo>() {
            @Override
            public Predicate toPredicate(Root<MessageDbo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.equal(root.get("isRevoked").as(Boolean.class),false);
                Predicate p2 = cb.like(root.get("companyCode").as(String.class), "%"+companyCode+"%");
                Predicate p3 = cb.between(root.get("salary").as(Double.class),salaryMin,salaryMax);
                query.where(cb.and(p3,cb.and(p1,p2)));
                return query.getRestriction();
            }
        };
        return messageRepo.findAll(specification,new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    public MessageDbo getMessage(Integer messageId) {
        return messageRepo.findAllById(messageId);
    }
}
